<template>
    <div class="topbar">
        <div class="warp topbarwarp">
            <p>欢迎来到叩丁狼积分商城</p>
            <ul class="loginnav">
                <li>
                    <img :src="userInfo.headImg" alt="" />
                    <span>用户名:{{ userInfo.nickName }}</span>
                </li>
                <li>我的鸡腿：{{ userInfo.coin }}</li>
                <li>获得鸡腿</li>
                <li>叩丁狼官网</li>
                <li class="cart-btn" v-if="isLogined">
                    <img src="../assets/img/cart.png" alt="" />
                    <span>购物车</span>
                    <strong>{{ cartTotal }}</strong>
                </li>
                <li class="login-btn" @click="changeIsShowLoginModal(true)" v-else>登录</li>
            </ul>
        </div>
    </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from 'vuex';
import { WeixinLoginApi } from '@/request/api';

export default {
    data() {
        return {};
    },
    created() {
        console.log('执行了topbar里面的钩子函数');
        this.$nextTick(async () => {
            let mycode = this.$route.query.code;
            // console.log(mycode);
            // console.log(this.$route);
            //当地址栏中没有获取到code的时候（表示就是手机验证码登录方式）
            if (!mycode) {
                let mytoken = localStorage.getItem('x-auth-token');
                this.changeIsLogined(Boolean(mytoken));
                //如果有token，更新用户信息
                if (mytoken) {
                    console.log('*更新');
                    this.asyncUpdateUserInfo();
                } else {
                    //如果没有token，初始化
                    console.log('初始化');
                    this.initUserInfo();
                }
                return;
            }

            //当有code，发起微信扫码登录请求
            let res = await WeixinLoginApi({
                code: mycode,
            });
            if (res.code == 0) {
                // alert('登录成功');
                this.asyncChangeIsShowToast({
                    msg: '登录成功',
                    type: 'success',
                });
                localStorage.setItem('x-auth-token', res['x-auth-token']);
                this.changeIsLogined(true);
                this.asyncUpdateUserInfo();
            } else if (res.code == 400) {
                // alert('请重新扫码登录');
                this.asyncChangeIsShowToast({
                    msg: '请重新扫码登录',
                    type: 'danger',
                });
                this.changeIsShowLoginModal(true);
            } else if (res.code == 407) {
                // alert('请使用手机号再登录一次');
                this.asyncChangeIsShowToast({
                    msg: '请使用手机号再登录一次',
                    type: 'danger',
                });
                this.changeIsShowLoginModal(true);
                localStorage.setItem('uuid', res.uuid);
            }
            this.$router.push(this.$route.path);
        });
    },

    computed: {
        ...mapState({
            isLogined: state => state.showlogin.isLogined,
            cartTotal: state => state.userinfo.cartTotal,
            userInfo: state => state.userinfo.userInfo,
        }),
    },
    methods: {
        ...mapMutations({
            changeIsShowLoginModal: 'showmodal/changeIsShowLoginModal',
            changeIsLogined: 'showlogin/changeIsLogined',
            initUserInfo: 'userinfo/initUserInfo',
        }),
        ...mapActions({
            asyncUpdateUserInfo: 'userinfo/asyncUpdateUserInfo',
            asyncChangeIsShowToast: 'showtoast/asyncChangeIsShowToast',
        }),
    },
    watch: {
        // '$route.path': {
        //     handler(newVal, oldVal) {
        //         console.log('$route.path', oldVal, newVal);
        //         let mytoken = localStorage.getItem('x-auth-token');
        //         //  if(mytoken){
        //         //      this.changeIsLogined(true)
        //         //  }else{
        //         //      this.changeIsLogined(false)
        //         //  }
        //         this.changeIsLogined(Boolean(mytoken));
        //     },
        // },
    },
};
</script>

<style lang="less" scoped>
.topbar {
    height: 40px;
    background: #242b39;
    color: #fffefe;
    line-height: 40px;
    font-size: 14px;
    font-family: SourceHanSansSC;
    font-weight: 300;
    .topbarwarp {
        display: flex;
        justify-content: space-between;
        .loginnav {
            display: flex;
            li {
                width: 124px;
                display: flex;
                justify-content: center;
                cursor: pointer;
                img {
                    width: 26px;
                    height: 26px;
                    padding-top: 8px;
                    border-radius: 50%;
                }
                &.login-btn {
                    background-color: #0a328e;
                }
                &.cart-btn {
                    width: 124px;
                    height: 40px;
                    background: #0a328e;
                    text-align: center;
                    img {
                        width: 20px;
                        height: 19px;
                        vertical-align: middle;
                    }
                    span {
                        margin: 0 8px;
                    }
                    strong {
                        width: 22px;
                        height: 22px;
                        line-height: 22px;
                        display: inline-block;
                        border-radius: 50%;
                        background-color: #fd604d;
                    }
                }
            }
        }
    }
}
</style>
